<template>
  <div class="container">
    <div class="center-box">
      <!-- 数据表头 -->
      <div class="goods-center">
        <div class="goods-top">
          <div class="goods-left">
            <label class="checkbox"
              ><input type="checkbox" class="vam" v-model="allChecked" /><i></i
              ><span>全选</span></label
            >
            <span>商品</span>
          </div>
          <ul class="goods-right">
            <li>单价</li>
            <li>数量</li>
            <li>小计</li>
            <li>操作</li>
          </ul>
        </div>
      </div>
      <!-- 购物车的商品 -->
      <div class="goods-data" v-if="!cartList.length">
        <span class="iconfont icon-sanjiaoxing_shang-copy"></span>
        <span class="span">暂无数据~~~~~~</span>
      </div>
      <div class="order-box1" v-else>
        <div class="order-middle">
          <div class="order-max" v-for="cart in cartList" :key="cart.id">
            <!-- 商品 -->
            <div class="order-top">
              <div class="order-left">
                <input
                  type="checkbox"
                  :checked="cart.isChecked"
                  @change="isCheckedApi(cart, 1 - cart.isChecked)"
                />
                <a href="#"
                  ><img class="img-a" :src="cart.imgUrl" alt="tupian"
                /></a>
                <div>
                  <span>{{ cart.skuName }}</span>
                  <!-- <a href="#">HUAWEI Mate Xs 2 8GB+256GB</a> -->
                  <!-- <span>雅黑 鸿蒙手机 雅黑 8GB+256GB 4G 全网通 官方标配</span> -->
                </div>
              </div>
              <div class="order-right">
                <nav class="nav-i nav-a">¥ {{ cart.cartPrice }}</nav>
                <nav class="nav-i">
                  <InputNumber
                    :min="0"
                    :max="20"
                    :value="cart.skuNum"
                    @addCart="counterNum(cart, $event)"
                  />
                </nav>
                <nav class="nav-i nav-a">
                  ¥ {{ cart.skuNum * cart.cartPrice }}
                </nav>
                <nav class="nav-i nav-b" @click="deleteGoods(cart.skuId)">
                  删除
                </nav>
              </div>
            </div>
            <div class="order-reuse">
              <div class="order-top reuse">
                <div class="order-left">
                  <a href="#"
                    ><img class="img-a img-f" :src="cart.imgUrl" alt="tupian"
                  /></a>
                  <div>
                    <span>{{ cart.skuName }}</span>
                    <!--  <a href="#">HUAWEI Mate Xs 2 8GB+256GB</a>
                    <span>雅黑 鸿蒙手机 雅黑 8GB+256GB 4G 全网通 官方标配</span> -->
                  </div>
                </div>
                <div class="order-right">
                  <nav class="nav-i">
                    <!-- <InputNumber :min="0" :max="20" :value="cart.skuNum" /> -->
                  </nav>
                  <nav class="nav-i nav-c">¥ 9999</nav>
                  <nav class="nav-i nav-b">删除</nav>
                </div>
              </div>
            </div>
            <div class="order-bottom">
              <span>
                <input type="checkbox" />
                <span class="discounts">优惠</span>
                <a href="#" class="hover-red">延长服务宝1年 ￥121.00</a>
                <span class="price">￥138.00</span>
              </span>
              <span class="text-underline"><a href="#">了解详情</a></span>
            </div>
          </div>
        </div>
      </div>
      <!-- 结算 -->
      <div class="order-footer">
        <div class="center-footer">
          <div class="clearfix">
            <div class="sc-total-control">
              <label class="checkbox"
                ><input
                  class="checked"
                  type="checkbox"
                  v-model="allChecked"
                /><i></i><span>全选<!----></span></label
              >
              <span class="delete" @click="clearAllSelected"
                >删除选中的商品</span
              >
            </div>
            <div class="sc-total-price">
              <div class="sc-total-price-left">
                <p>
                  <label>总计：</label><span>¥ {{ totalPrice }}</span
                  ><em><b>不含运费</b></em>
                </p>
                <div class="total-choose">
                  已选择 <em>1</em>件商品，优惠： <span>¥&nbsp;888.00</span>
                </div>
              </div>
              <div class="sc-total-btn">
                <!-- <router-link to="/trade">立即结算</router-link> -->
                <button @click="countGoods">立即结算</button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import debounce from "lodash/debounce";
import InputNumber from "./components/InNumber.vue";
import { mapState } from "vuex";
import {
  getShopCartApi,
  getIsCheckedApi,
  deleteGoodsApi,
  addToCart,
} from "@/api/shopcartApi";

export default {
  name: "GoodsOrder",
  components: {
    InputNumber,
  },
  data() {
    return {
      cartList: [],
    };
  },
  mounted() {
    this.shopcartList();
  },
  methods: {
    //  获取数据
    async shopcartList() {
      const result = await getShopCartApi();
      this.cartList = result[0] ? result[0].cartInfoList : [];
    },
    async isCheckedApi(cart, isChecked) {
      cart.isChecked = isChecked;
      await getIsCheckedApi(cart.skuID, isChecked);
      this.shopcartList();
    },
    // 删除商品
    async deleteGoods(skuId) {
      await deleteGoodsApi(skuId);
      this.shopcartList();
    },
    // 删除选中的商品
    clearAllSelected() {
      this.cartList.forEach(async (item) => {
        if (item.isChecked) {
          this.deleteGoods(item.skuId);
        }
      });
    },

    // 添加数量
    counterNum: debounce(async function (cart, newValue) {
      const num = newValue - cart.skuNum;
      await addToCart(cart.skuId, num);
      // this.shopcartList();
      cart.skuNum = newValue;
    }, 500),

    // 计数器的请求
    // async counterNum(cart, newValue) {
    //   const num = newValue - cart.skuNum;
    //   await addToCart(cart.skuId, num);
    //   this.shopcartList();
    //   cart.skuNum = num;
    // },

    // 点击立即结算
    countGoods() {
      if (!this.token) {
        this.$router.history.push("/login");
        return;
      }
      this.$router.history.push("/trade");
    },
  },
  computed: {
    ...mapState("user", ["token"]),

    // 计算总数
    totalPrice() {
      return this.cartList.reduce((p, c) => {
        return p + (c.isChecked ? c.skuNum * c.skuPrice : 0);
      }, 0);
    },
    // 计算全选
    allChecked: {
      get() {
        return this.cartList.every((item) => {
          return item.isChecked === 1;
        });
      },
      set(newVal) {
        let isChecked = newVal ? 1 : 0;
        this.cartList.forEach((item) => {
          if (item.isChecked === isChecked) return;
          this.isCheckedApi(item, isChecked);
        });
      },
    },
  },
};
</script>

<style lang="less" scoped>
.container {
  width: 100%;
  background-color: #f5f5f5;
  .center-box {
    width: 1200px;
    margin: auto;
    padding: 20px 0;
    .goods-top {
      height: 40px;
      width: 100%;
      display: flex;
      justify-content: space-between;
      background-color: #ffffff;
      .goods-left {
        line-height: 40px;
        span {
          display: inline-block;
          width: 150px;
          font-size: 12px;
          color: #a4a4a4;
        }
        .checkbox {
          margin-left: 40px;
          .vam {
            vertical-align: middle;
          }
        }
      }

      .goods-right {
        overflow: hidden;
        li {
          float: left;
          width: 100px;
          line-height: 40px;
          margin-right: 30px;
          text-align: center;
          font-size: 12px;
          color: #a4a4a4;
        }
      }
    }
  }
}
.order-box1 {
  margin: 20px 0;
}
.order-max {
  background-color: #ffffff;
  padding: 20px 40px 20px 40px;
}

.order-top {
  overflow: hidden;
  display: flex;
  justify-content: space-between;
}
.order-left {
  display: flex;
  font-size: 14px;
  a {
    display: block;
    font-size: 14px;
    color: #3a3a3a;
  }
  span {
    color: #666666;
  }
  .img-a {
    width: 100px;
    height: 100px;
  }
}
.order-right {
  display: flex;
  .nav-i {
    width: 100px;
    text-align: center;
    font-size: 14px;
    color: #3a3a3a;
  }
  .nav-a {
    color: #ca141d;
  }
  .nav-b {
    color: #a4a4a4;
    padding-right: 40px;
    cursor: pointer;
  }
  .nav-c {
    color: #ca141d;
    text-decoration: line-through;
  }
}
.order-reuse {
  .reuse {
    padding: 20px 0;
    background-color: #f5f5f5;
    .img-f {
      width: 50px;
      height: 50px;
    }
  }
}
.order-bottom {
  height: 48px;
  padding: 0 35px 0 0px;
  background: #ffffff;
  border: solid 1px #d6d6d6;
  font-size: 15px;
  line-height: 48px;
  margin-top: 16px;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  .discounts {
    display: inline-block;
    width: 52px;
    height: 20px;
    line-height: 20px;
    color: red;
    text-align: center;
    border: 1px solid red;
    margin-right: 6px;
  }
  .price {
    margin-left: 10px;
    color: #a4adc4;
    text-decoration: line-through;
  }
  .text-underline {
    text-decoration: underline;
  }
  &:hover .hover-red {
    color: red;
  }
}
.order-footer {
  margin: 20px 0;
  background: #fff;
  .center-footer {
    padding: 0 40px;

    .clearfix {
      overflow: hidden;
      .sc-total-control {
        margin-top: 20px;
        float: left;
        .delete {
          cursor: pointer;
          color: lightsalmon;
          margin-left: 10px;
        }
      }
      .sc-total-price {
        float: right;
        .sc-total-btn {
          button {
            float: right;
            width: 180px;
            height: 60px;
            line-height: 60px;
            background: #ca151e;
            font-size: 18px;
            color: #fff;
            text-align: center;
          }
        }
        .sc-total-price-left {
          padding-right: 30px;
          margin-top: 14px;
          text-align: right;
          float: left;
          p {
            label {
              font-size: 14px;
            }
            span {
              color: red;
              padding-right: 5px;
            }
            b {
              font-size: 14px;
              color: #a4a4a4;
            }
          }
          .total-choose {
            margin: 5px 0;
            font-size: 12px;
            color: #a4a4a4;
          }
        }
      }
    }
  }
}
.goods-data {
  background-color: #fff;
  position: relative;
  .span {
    margin: 20px auto;
    display: block;
    width: 150px;
  }
  .icon-sanjiaoxing_shang-copy {
    position: absolute;
    top: -19px;
    left: 46%;
    font-size: 30px;
  }
}
</style>